# -*— coding: utf-8 -*-
# @Time: 2024/6/17 11:14
# @Author :Lin Zhou
# @Email : lin1013143@163.com
# @File : paginatedViewMixin.py
# @SoftWare: PyCharm

from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response

from common.standard_pagination import StandardPagination
from utils.response_body_utils import get_success_response


class PaginatedViewMixin:
    pagination_class = StandardPagination  # 假设 StandardPagination 已在某处定义

    def base_page(self, request, get_queryset):
        """
        分页接口默认获取返回结果，通用代码
        :param request:
        :param get_queryset:
        :return:
        """
        page = self.paginate_queryset(get_queryset)
        if page is not None:
            serializer = self.get_serializer(page, many=True)
            data = serializer.data
        else:
            data = self.get_serializer(get_queryset, many=True).data

        page_size = self.pagination_class.page_size
        page_num = int(request.query_params.get("page_num", 1))
        size = int(request.query_params.get("page_size", page_size))
        total_count = get_queryset.count()
        max_pages = (total_count + size - 1) // size

        res_data = {
            "list": data,
            "page_size": size,
            "page_num": page_num,
            "total_count": total_count,
            "max_pages": max_pages
        }
        return get_success_response(res_data)  # 假设 get_success_response 已在某处定义
